#include "tankmove.h"

extern unsigned char gImage_tank52_60[];

/*确定tank的初始位置*/
void tankInitial(struct tank *p, unsigned char *A){

    p->direction = 0;
    lcd_drawPic_up(52, 60, p->x, p->y, A);

}


/*tank向上移动*/
void moveUp(struct tank *p, unsigned char *A){
    lcd_drawRegionBlack(p->x, p->y, (p->x + 62), (p->y + 60));
    p->x = p->x;
    p->y = p->y - 30;
    lcd_drawPic_up(52, 60, p->x, p->y, A);

}

/*tank向下移动*/
void moveDown(struct tank *p, unsigned char *A){
    lcd_drawRegionBlack(p->x, p->y, (p->x + 62), (p->y + 60));
    p->x = p->x;
    p->y = p->y + 30;
    //lcd_drawPic(52, 60, p->x, p->y, A);
    lcd_drawPic_down(52, 60, p->x, p->y, A);

}
/*tank向左移动*/
void moveLeft(struct tank *p, unsigned char *A){
    lcd_drawRegionBlack(p->x, p->y, (p->x + 62), (p->y + 60));
    p->x = p->x - 30;
    p->y = p->y;
    lcd_drawPic_left(60, 52, p->x, p->y, A);

}

/*tank向右移动*/
void moveRight(struct tank *p, unsigned char *A){
    lcd_drawRegionBlack(p->x, p->y, (p->x + 62), (p->y + 60));
    p->x = p->x + 30;
    p->y = p->y;
    lcd_drawPic_right(60, 52, p->x, p->y, A);

}

/*tank不移动*/
void notMove(struct tank *p, unsigned char *A){
    lcd_drawPic_up(52, 60, p->x, p->y, A);
}

/*结构体函数，返回的是一个tank类型的结构体，用于创建tank结构体*/
struct tank* tankCreate(char* id, int initialX, int initialY){
    struct tank* mytank = malloc(sizeof(struct tank));
    assert(mytank != NULL);

    mytank->id = strdup(id);
    mytank->x  = initialX;
    mytank->y  = initialY;
    mytank->direction = 0;

    return mytank;


}

/*
**子弹创建函数
**传入的tank型的结构体，子弹的结构体数组和子弹的数量
*/
void bulletCreate(struct tank *p, struct bullet bullet[BULLET_NUM], int *bullet_num){
    bullet[*bullet_num].direction = p->direction;
    switch (bullet[*bullet_num].direction){
        case 0:{
            bullet[*bullet_num].x = p->x + 23;
            bullet[*bullet_num].y = p->y - 12;
            lcd_drawRegionRed(bullet[*bullet_num].x, bullet[*bullet_num].y, bullet[*bullet_num].x + 7, bullet[*bullet_num].y + 7);
            break;
        }

        case 1:{
            bullet[*bullet_num].x = p->x + 23;
            bullet[*bullet_num].y = p->y + 66;
            lcd_drawRegionRed(bullet[*bullet_num].x, bullet[*bullet_num].y, bullet[*bullet_num].x + 7, bullet[*bullet_num].y + 7);
            break;
        }
        case 2:{
            bullet[*bullet_num].x = p->x - 12;
            bullet[*bullet_num].y = p->y + 23;
            lcd_drawRegionRed(bullet[*bullet_num].x, bullet[*bullet_num].y, bullet[*bullet_num].x + 7, bullet[*bullet_num].y + 7);
            break;
        }
        case 3:{
            bullet[*bullet_num].x = p->x + 66;
            bullet[*bullet_num].y = p->y + 23;
            lcd_drawRegionRed(bullet[*bullet_num].x, bullet[*bullet_num].y, bullet[*bullet_num].x + 7, bullet[*bullet_num].y + 7);
            break;
        }
    }
    bullet[*bullet_num].exist = 1;
    bullet[*bullet_num].isMy = 1;
    *bullet_num = (*bullet_num) + 1;
    if(*bullet_num == BULLET_NUM){
        *bullet_num = 0;
    }    

}

/*
**x,y分别是传入的子弹的横纵坐标
**返回值:1代表子弹仍然存在，0代表子弹不存在
*/
int bulletCheck(int x, int y){
    if((x >= 30) && (x <= 994) && (y >= 96) && (y <= 738)){
        return 1;
    }
    else{
        return 0;
    }

}

/*
**子弹移动函数
**传入的是子弹的结构体数组
*/
void bulletMove(struct bullet bullet[BULLET_NUM]){
    
    int i, j = 0;
    for(i = 0; i < 20; i++){
        bullet[i].exist = bulletCheck(bullet[i].x, bullet[i].y);
        if(bullet[i].exist == 1){
            lcd_drawRegionBlack(bullet[i].x, bullet[i].y, bullet[i].x + 7, bullet[i].y + 7);
            switch (bullet[i].direction){
                case 0:{
                    bullet[i].x = bullet[i].x;
                    bullet[i].y = bullet[i].y - 20;
                    break;
                }

                case 1:{
                    bullet[i].x = bullet[i].x;
                    bullet[i].y = bullet[i].y + 20;
                    break;
                }

                case 2:{
                    bullet[i].x = bullet[i].x - 20;
                    bullet[i].y = bullet[i].y;
                    break;

                }
                case 3:{
                    bullet[i].x = bullet[i].x + 20;
                    bullet[i].y = bullet[i].y;
                    break;
                }
                default:{
                    break;
                }

            }
            lcd_drawRegionRed(bullet[i].x, bullet[i].y, bullet[i].x + 7, bullet[i].y + 7);
        }
    }
}   


/*
**子弹摧毁函数
**传入的是子弹的结构体数组
*/
void bulletDestroy(struct bullet bullet[BULLET_NUM]){
    int i, j = 0;
    for(i = 0; i < 20; i++){
        if(bullet[i].exist == 0){
            lcd_drawRegionBlack(bullet[i].x, bullet[i].y, bullet[i].x + 7, bullet[i].y + 7);
        }
        else {
            continue;
        }
    }
}